package com.example.demo.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Map;
import com.example.demo.mapper.MapMapper;
import com.example.demo.service.MapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Atanycosts
 * @description 针对表【map】的数据库操作Service实现
 * @createDate 2023-05-08 20:25:10
 */
@Service
public class MapServiceImpl extends ServiceImpl<MapMapper, Map>
        implements MapService {

    @Autowired
    @Lazy
    private MapService mapService;

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public List<Map> getALl() {
        List<Map> list = mapService.list();
        return list;
    }

    /**
     * 根据ID查
     *
     * @param id
     * @return
     */
    @Override
    public Map getByID(Long id) {

        Map byId = mapService.getById(id);

        return byId;
    }

    /**
     * 增加人数
     *
     * @param map
     * @return
     */
    @Override
    @Transactional
    public void updateMap(Map map) {

        LambdaQueryWrapper<Map> mapLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (map.getId() != null && map.getName() == null) {

            mapLambdaQueryWrapper.eq(Map::getId, map.getId());
            Map one = mapService.getOne(mapLambdaQueryWrapper);

            one.setValue(one.getValue() + 1);
            mapService.updateById(one);
        } else if (map.getName() != null && map.getId() == null) {

            mapLambdaQueryWrapper.eq(Map::getName, map.getName());
            Map one = mapService.getOne(mapLambdaQueryWrapper);

                one.setValue(one.getValue() + 1);
            mapService.updateById(one);
        }

    }

}




